﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using T4UtilityBelt.UxDesigns.Controls;
using T4UtilityBelt.UxDesigns.Screens.Renders;
using T4UtilityBelt.Writers;

namespace T4UtilityBelt.UxDesigns.Screens
{
    public class EntityListScreen : EntityScreen<EntityListScreen, IEntityListScreenRenderer>
    {
        public EntityListScreen()
        {
            StandardConstructor();
        }

        public EntityListScreen(string entityName)
            : base(entityName)
        {
            StandardConstructor();
        }

        public EntityListScreen(string entityName, string screenName)
            : base(entityName, screenName)
        {
            StandardConstructor();
        }

        private void StandardConstructor()
        {
            //FilterControls.DefaultPropertyDataContext = "ListFilter";
            //Controls.DefaultPropertyDataContext = "Entry";
            //Controls.DefaultIsEnabledProperty = "CanUpdate";

            _mainGrid = new GridControl();
            
            GridRow row = new GridRow();
            _mainGrid.Rows.Add(row);
            _titlePanel = new StackPanelControl();
            row.Controls.Add(_titlePanel);

            LabelControl title = new LabelControl {Text = this.Name};

            _titlePanel.Controls.Add(title);

            row = new GridRow();
            _mainGrid.Rows.Add(row);
            _listBox = new ListBoxControl();
                //{
                //    Name = "listBox", 
                //    ItemSelectedCommand = "ItemSelectedCommand"
                //};
            row.Controls.Add(_listBox);

            row = new GridRow();
            _mainGrid.Rows.Add(row);
            _buttonsPanel = new StackPanelControl();
            row.Controls.Add(_buttonsPanel);

            ButtonControl button = new ButtonControl { Click = "AddCommand", Text = "Add" };
            _buttonsPanel.Controls.Add(button);

            button = new ButtonControl { Click = "RefreshCommand", Text = "Refresh" };
            _buttonsPanel.Controls.Add(button);

            Controls.Add(_mainGrid);
        }

        private GridControl _mainGrid;
        public GridControl MainGrid
        {
            get { return _mainGrid; }
        }

        private StackPanelControl _titlePanel;
        public StackPanelControl TitlePanel
        {
            get { return _titlePanel; }
        }

        private ListBoxControl _listBox;
        public ListBoxControl ListBox
        {
            get { return _listBox; }
        }

        private StackPanelControl _buttonsPanel;
        public StackPanelControl ButtonPanel
        {
            get { return _buttonsPanel; }
        }

        #region FilterControls

        private readonly ControlCollection _filterControls = new ControlCollection();
        public ControlCollection FilterControls
        {
            get { return _filterControls; }
        }

        public IEnumerable<Control> AllFilterControls()
        {
            if (FilterControls.Count > 0)
            {
                foreach (var c in FilterControls)
                {
                    yield return c;
                }
            }
            else
            {
                foreach (var p in Entity.Properties)
                {
                    yield return Control.Create(p);
                }
            }
        }

        #endregion FilterControls

        internal override void FixModel(T4UtilityBelt.DataDesigns.EntityModel model)
        {
            base.FixModel(model);

            if (Entity != null)
            {
                foreach (Control ctl in FilterControls)
                {
                    ctl.FixModel(Entity);
                }
            }
        }
    }
}